﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace mds.DataAccessService
{
    internal class Util
    {
        /// <summary>
        /// 分库的链接字符串形式:数据库名_{0}的模板化配置
        /// 而且主键必须是整数和长整数
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        internal static string ParseCon(string connectionString, List<SqlDataParmeter> parmeters)
        {
            if (parmeters == null) return connectionString;
            var pars = parmeters.FindAll(a => a.IsPrimarykey);

            if (pars.Count == 1)
            {
                var dbtype = pars[0];
                if ((dbtype.ParamterType == DbType.Int32) || (dbtype.ParamterType == DbType.Int64))
                {
                    long id = 0;
                    long.TryParse(dbtype.ToString(), out id);
                    if (id == 0) return string.Format(connectionString, "1"); ;
                    return string.Format(connectionString, id.ToString().Substring(0, 1));
                }
                else {
                    return string.Format(connectionString, dbtype.ToString().GetHashCode()%10);
                }
            }

            return connectionString;
        }
    }
}